package util;

import java.util.Stack;

/**
 * Created by YanYang on 2015/12/27.
 */
public class NumberTransfer {
    private static char[] charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
    /*
     * @time 2015-12-27
     * @content将10进制转化为62进制
     * @param number
     * @return 转化为62进制的字符串表示
     */
    public static String _10to62(long number){
        Long rest = number;
        Stack<Character> stack = new Stack<Character>();
        StringBuilder result = new StringBuilder();
        while(rest != 0){
            stack.push(charset[new Long(rest-(rest/62)*62).intValue()]);
            rest = rest/62;
        }
        while(!stack.isEmpty()){
            result.append(stack.pop());
        }
        return result.toString();
    }

    public static long _62to10(String ident62){
        char[] char62 = ident62.toCharArray();
        long decimal = 0;
        int base = 62;
        long keisu = 0; //进制幂次方（如62^2）
        int cnt = 0;
        for(int i = char62.length-1; i >= 0; i--){
            int num = 0;
            if(char62[i] > 48 && char62[i] <=57){
                num = char62[i]-48;
            }else if(char62[i] >= 65 && char62[i] <= 90){
                num = char62[i] - 65 + 10;
            }else if(char62[i] >= 97 && char62[i] <= 122){
                num = char62[i] - 97 + 10 + 26;
            }
            keisu = (long) Math.pow((double)base, (double) cnt);
            decimal += num * keisu;
            cnt++;
        }
        return decimal;
    }

    public static void main(String[] args){
        System.out.println(_10to62(120));
    }
}
